package com.xiaodu.acer.phoneplay.presenter;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xiaodu.acer.phoneplay.bean.AppInfo;
import com.xiaodu.acer.phoneplay.common.Constant;
import com.xiaodu.acer.phoneplay.common.apkparset.AndroidApk;
import com.xiaodu.acer.phoneplay.common.rx.subscriber.ProgressSubcriber;
import com.xiaodu.acer.phoneplay.common.rx.subscriber.RxSchedulers;
import com.xiaodu.acer.phoneplay.presenter.contract.AppManagerContract;
import com.xiaodu.acer.phoneplay.ui.base.BasePresenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import library.util.ACache;
import zlc.season.rxdownload2.RxDownload;
import zlc.season.rxdownload2.entity.DownloadFlag;
import zlc.season.rxdownload2.entity.DownloadRecord;


public class AppManagerPresenter extends BasePresenter<AppManagerContract.Model, AppManagerContract.View> {

    @Inject
    public AppManagerPresenter(AppManagerContract.Model model, AppManagerContract.View view) {
        super(model, view);
    }

    public void getDownLoadingApps() {
        mModel.getDownloadRecord()
                .compose( RxSchedulers.<List<DownloadRecord>>io_main())
                .subscribe(new ProgressSubcriber<List<DownloadRecord>>(mContext, mView) {
                    @Override
                    public void onNext(List<DownloadRecord> downloadRecords) {
                        mView.showDownloading(downloadRecordFilter(downloadRecords));
                    }
                });
    }

    /**
     * 过滤取出所有已安装的包放入新的list结合中
     *
     * @param mList 所有数据
     * @return 所有已安装的包
     */
    private List<DownloadRecord> downloadRecordFilter(List<DownloadRecord> mList) {
        List<DownloadRecord> newList = new ArrayList<>();
        for (DownloadRecord record : mList) {
            if (record.getFlag() != DownloadFlag.COMPLETED) {
                newList.add(record);
            }
        }
        return newList;
    }

    public void getLocalApks() {
        mModel.getLocalApks()
                .compose(RxSchedulers.<List<AndroidApk>>io_main())
                .subscribe(new ProgressSubcriber<List<AndroidApk>>(mContext, mView) {
                    @Override
                    public void onNext(List<AndroidApk> androidApks) {
                        mView.showApps(androidApks);
                    }
                });
    }

    public void getInstalledApps() {
        mModel.getInstalledApps()
                .compose(RxSchedulers.<List<AndroidApk>>io_main())
                .subscribe(new ProgressSubcriber<List<AndroidApk>>(mContext, mView) {
                    @Override
                    public void onNext(List<AndroidApk> androidApks) {
                        mView.showApps(androidApks);
                    }
                });
    }

    // TODO: 2018/1/16 需要升级的app，不太确定
    public void getUpdateApps() {
        String json = ACache.get(mContext).getAsString(Constant.APP_UPDATE_LIST);

        //如果取到的json串不是空的
        if (!TextUtils.isEmpty(json)) {
            //把json串转换成List集合
            Gson gson = new Gson();
            List<AppInfo> apps = gson.fromJson(json, new TypeToken<List<AppInfo>>() {
            }.getType());

            Observable.just(apps)
                    .compose(RxSchedulers.<List<AppInfo>>io_main())
                    .subscribe(new ProgressSubcriber<List<AppInfo>>(mContext, mView) {
                        @Override
                        public void onNext(List<AppInfo> appInfos) {
                            mView.showUpdateApps(appInfos);
                        }
                    });
        }

    }

    public RxDownload getRxDowanload() {
        return mModel.getRxDownload();
    }


}
